<template>
  <el-dialog :title="!dataForm.attributeId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="关联商品" prop="productId">
        <ProductAutocomplete v-model.number="dataForm.productId"></ProductAutocomplete>
        <!-- <span v-if="dataForm.productId">当前选中的为 ： {{ productTitle }} ({{ dataForm.productId }}) </span> -->
      </el-form-item>
      <el-form-item label="属性名称" prop="attributeName">
        <el-input v-model="dataForm.attributeName" placeholder="属性名称（如：产地）"></el-input>
      </el-form-item>
      <el-form-item label="属性值" prop="attributeValue">
        <el-input v-model="dataForm.attributeValue" placeholder="属性值（如：中国）"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import ProductAutocomplete from "@/views/modules/common/product-autocomplete"


export default {

  data() {
    return {
      visible: false,
      productTitle: '',
      dataForm: {
        attributeId: 0,
        productId: 0,
        attributeName: '',
        attributeValue: ''
      },
      dataRule: {
        productId: [
          { required: true, message: '关联商品ID不能为空', trigger: 'blur' }
        ],
        attributeName: [
          { required: true, message: '属性名称（如：产地）不能为空', trigger: 'blur' }
        ],
        attributeValue: [
          { required: true, message: '属性值（如：中国）不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    ProductAutocomplete
  },
  methods: {
    init(id) {
      this.dataForm.attributeId = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.attributeId) {
          await this.$http({
            url: this.$http.adornUrl(`/product/productattribute/info/${this.dataForm.attributeId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.productId = data.data.productId
              this.dataForm.attributeName = data.data.attributeName
              this.dataForm.attributeValue = data.data.attributeValue
            }
          })


        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/productattribute/${!this.dataForm.attributeId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'attributeId': this.dataForm.attributeId || undefined,
              'productId': this.dataForm.productId,
              'attributeName': this.dataForm.attributeName,
              'attributeValue': this.dataForm.attributeValue
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },

    //   getProductTitle(title) {
    //     console.log("getProductTitle", title);

    //     this.productTitle = title
    //   }
  }
}
</script>
